﻿
/// <reference path="vswd-ext_2.2.js" />
/********************poi.js*********************************
*Create Date: 2012-2
*Description : For record polygon geofence
*********************************************************************/

function AddNewPolyFence(name,points) {
    
}


function PolygonGeofenceWindow() {
    if (Ext.getCmp('polygon_fence_window')) {
        Ext.getCmp('polygon_fence_window').show();
        return;
    }
    var pgeofencefields = ["polygon_geofence_id", "points", "create_account", "create_type", "name"];
    var pgeofencestore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchPolygonFence",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: pgeofencefields,
            root: "data"
        }),
        autoLoad: true
    });

    var txtKey = new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        emptyText: "",
        maxText: _Message_MaxText + ' 32'
    });

    var tbar = new Ext.Toolbar({
        items: [
            txtKey, {
                tooltip: { text: _POI_Search, autoHide: true },
                icon: 'images/search16.gif',
                handler: function() {
                    pgeofencestore.reload({
                        params: {
                            namekey: txtKey.getValue()
                        }
                    });
                }
            }, {
                xtype: 'tbseparator', width: 5
            }, {
                tooltip: { text: _TxtTipAddGeofence, autoHide: true },
                icon: 'images/add.png',
                handler: function() {
                    pgeofencewindow.minimize();
                    imap.startDrawPolygonGeofence(0, function(points) {
                        var name = document.getElementById('txt_polygon_fence_name').value;
                        Ext.Ajax.request({
                            url: 'HandlerADUPolygonFence',
                            method: 'POST',
                            success: function(response) {
                                var responseArray = Ext.util.JSON.decode(response.responseText);
                                var flag = responseArray.success;
                                if (flag == true) {
                                    //刷新报表
                                    pgeofencestore.reload({
                                        params: {
                                            namekey: ''
                                        }
                                    });
                                    imap.closePolygonAndInfoWindow();
                                    pgeofencewindow.show();
                                }
                                else {
                                    msgWarning(_NewForText_Failure);
                                }
                            },
                            failure: function(response) {
                                msgWarning(_NewForText_Failure);
                            },
                            params: {
                                type: 'add',
                                name: name,
                                points: points
                            }
                        });
                    });
                }
            }, {
                tooltip: { text: _TxtTipEditGeofence, autoHide: true },
                icon: 'images/edit.gif',
                handler: function() {
                    if (imap.getEditPolygonFenceStatus() == true) {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _TxtIsEditingFence,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                    else {
                        if (grid.getSelectionModel().getCount() == 1) {
                            imap.showCanEditPolygonFence(grid.getSelectionModel().getSelections()[0].get('points'), function(points) {
                                var name = document.getElementById('txt_polygon_fence_name').value;
                                Ext.Ajax.request({
                                    url: 'HandlerADUPolygonFence',
                                    method: 'POST',
                                    success: function(response) {
                                        var responseArray = Ext.util.JSON.decode(response.responseText);
                                        var flag = responseArray.success;
                                        if (flag == true) {
                                            //刷新报表
                                            pgeofencestore.reload({
                                                params: {
                                                    namekey: ''
                                                }
                                            });
                                            imap.hideCanEditPolygonFence();
                                            pgeofencewindow.show();
                                        }
                                        else {
                                            msgWarning(_NewForText_Failure);
                                        }
                                    },
                                    failure: function(response) {
                                        msgWarning(_NewForText_Failure);
                                    },
                                    params: {
                                        type: 'update',
                                        name: name,
                                        polygon_geofence_id: grid.getSelectionModel().getSelections()[0].get('polygon_geofence_id'),
                                        points: points
                                    }
                                });
                            });
                        }
                        else {
                            Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                            Ext.Msg.show({
                                title: _TrackerMain_Title,
                                msg: _POI_SelectOne,
                                buttons: Ext.Msg.OK,
                                animEl: 'elId',
                                icon: Ext.MessageBox.INFO
                            });
                        }
                    }
                }
            }, {
                tooltip: { text: _TxtTipDeleteGeofence, autoHide: true },
                icon: 'images/delete.png',
                handler: function() {
                    if (grid.getSelectionModel().getCount() == 1) {
                        Ext.MessageBox.YESNO = { yes: _TrackerMain_Yes, no: _TrackerMain_No };
                        Ext.MessageBox.confirm(_TrackerMain_Title, _Photo_SureToDelete, function(btn) {
                            var id = grid.getSelectionModel().getSelections()[0].data.polygon_geofence_id;
                            if (btn == "yes") {
                                Ext.Ajax.request({
                                    url: 'HandlerADUPolygonFence',
                                    success: function(a, action) {
                                        var flag = Ext.util.JSON.decode(a.responseText).success;
                                        if (flag == true) {
                                            Ext.MessageBox.hide();
                                            msgWarning(_CustomerConfigResource_Deleted);
                                            imap.hideCannotEditPolygonFence(id)
                                            pgeofencestore.reload({
                                                params: {
                                                    namekey: ''
                                                }
                                            });
                                        }
                                        else {
                                            msgWarning(_PictureResource_Deletefailed);
                                        }
                                    },
                                    failure: function() {
                                        msgWarning(_PictureResource_Deletefailed);
                                    },
                                    params: {
                                        ids: id,
                                        type: 'delete'
                                    }
                                });
                            }
                        }, this);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectAtLeastOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            }, {
                icon: 'images/view.gif',
                handler: function() {
                    var count = grid.getStore().getCount();
                    var viewModel = imap.getAndSetViewPolygonModel();
                    if (viewModel == true) {
                        for (var i = 0; i < count; i++) {
                            var record = grid.getStore().getAt(i);
                            imap.showCannotEditPolygonFence(record.get('points'), record.get('polygon_geofence_id'), false, record.get('name'));
                        }
                    }
                    else {
                        imap.hideAllCannotEditPolygonFence();
                    }
                }
            }

        ]
    });

    var grid = new Ext.grid.GridPanel({
        store: pgeofencestore,
        sm: new Ext.grid.CheckboxSelectionModel({ dataIndex: "polygon_geofence_id" }),
        viewConfig: {
            forceFit: true
        },
        enableHdMenu: false,
        autoScroll: true,
        columns: [
                { header: '', width: 50, hidden: true, sortable: true, menuDisabled: true, dataIndex: 'polygon_geofence_id' },
                { header: _TxtGeofenceName, width: 120, sortable: true, menuDisabled: true, dataIndex: 'name' },
                { header: '', width: 50, hidden: true, sortable: true, menuDisabled: true, dataIndex: 'points' }
        //                ,{ header: _DriverResource_FirstLicense, width: 150, sortable: true, menuDisabled: true, dataIndex: 'driver_license_model1' }
            ],
        width: 280,
        height: 200
    });

    grid.addListener('rowdblclick', function(obj, index) {
        var record = grid.getStore().getAt(index);
        //显示多边形
        imap.showCannotEditPolygonFence(record.get('points'), record.get('polygon_geofence_id'), true, record.get('name'));
    });

    var pgeofencewindow = new Ext.Window({
        title: _TxtPolygonFenceReport,
        id: 'polygon_fence_window',
        iconCls: 'icon-report-polygon-fence',
        layout: 'fit',
        height: 250,
        width: 320,
        items: grid,
        tbar: tbar,
        minimizable: true,
        constrain:true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(pgeofencewindow);
            }
        }
    });

    pgeofencewindow.show();
}